﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhsystem.Main;
using dhSystemBLL.produceBLL;
using baseSet.importExport;
using dhDataAcess.dataAcess;
using dhDataAcess.dataAcess.produceDataSetTableAdapters;
using FastReport;
using baseSet.power;
using dhDataAcess.baseBLL;

namespace dhsystem.produceForm
{
    public partial class FrmorderStockOutMaster : Form
    {
        orderStockOutBLL orderStockOut = new orderStockOutBLL();
        string state;
        public FrmorderStockOutMaster()
        {
            InitializeComponent();
        }
        //打开窗口时显示列表数据
        private void FrmorderStockOutMaster_Load(object sender, EventArgs e)
        {
            getData();
        }
        //新增按钮，显示新增数据组件
        private void toolAdd_Click(object sender, EventArgs e)
        {
            state = "新增";
            FrmorderStockOutMasterAdd orderStockOutMasterAdd = new FrmorderStockOutMasterAdd(bindingSourceorderStockOutMaster, state);
            orderStockOutMasterAdd.ShowDialog();
        }
        //工具栏查询按钮，查询所有列表数据
        private void toolQuery_Click(object sender, EventArgs e)
        {
            FrmDataQuery query = new FrmDataQuery(moduleInfo.orderStockOutMaster);
            DialogResult result = query.ShowDialog();
            if (result == DialogResult.Yes)
            {
                dGridorderStockOutDetail.DataSource = query.rTable;
            }
        }
        //工具栏的编辑按钮,修改数据
        private void toolEdit_Click(object sender, EventArgs e)
        {
            state = "编辑";
            FrmorderStockOutMasterAdd orderStockOutMasterAdd = new FrmorderStockOutMasterAdd(bindingSourceorderStockOutMaster, state);
            orderStockOutMasterAdd.ShowDialog();
        }
        //删除按钮，删除选中行数据
        private void toolDel_Click(object sender, EventArgs e)
        {
            //string billNo = dGridorderStockOutMaster.Rows[dGridorderStockOutMaster.CurrentRow.Index].Cells[1].Value.ToString();
            //try
            //{
            //    DialogResult result = MessageBox.Show("是否删除：" + billNo + "！", "提示信息", MessageBoxButtons.YesNo);
            //    if (result == DialogResult.Yes)
            //    {

            //        bindingSourceorderStockOutMaster.DataSource = orderStockOut.orderStockOutMasterDel(billNo);

            //    }
            //    getData();
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message.ToString());
            //}
            string billNo = dGridorderStockOutMaster.Rows[dGridorderStockOutMaster.CurrentRow.Index].Cells[1].Value.ToString();
            try
            {
                DialogResult result = MessageBox.Show("是否删除：" + billNo + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {


                    produceDataSet.orderStockOutDetailDataTable ptable = orderStockOut.getsorderStockOutDetailAllBybillNo(billNo);
                    foreach (produceDataSet.orderStockOutDetailRow Row in ptable)
                    {
                        if (Convert.ToDecimal(basicSql.getV_orderStockOutSUM(Row.orderDetailNo)) == 0)
                        {
                            basicSql.getV_orderStockOutUpdate(Row.orderDetailNo, "新建");//更新出库记录状态
                            basicSql.getVpackStockInSummarkUpdate(Row.orderDetailNo, "新建");//更新包装记录状态
                            basicSql.getorderDetailStatusUpdateI(Row.orderDetailNo);//更新订单明细状态
                        }
                        else
                        {
                            basicSql.getV_orderStockOutUpdate(Row.orderDetailNo, "开始出库");//更新出库记录状态
                            basicSql.getVpackStockInSummarkUpdate(Row.orderDetailNo, "新建");//更新包装记录状态
                            basicSql.getorderDetailStatusUpdateI(Row.orderDetailNo);//更新订单明细状态
                        }
                        basicSql.getVpackStockInSummarkUpdate(Row.orderDetailNo, "新建");
                        bindingSourceorderStockOutMaster.DataSource = orderStockOut.orderStockOutMasterDel(billNo);

                    }
                }
                getData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //查询按钮，按条件查询数据
        private void buttonQuery_Click(object sender, EventArgs e)
        {
             try
            {
                bindingSourceorderStockOutDetail.DataSource = orderStockOut.getsorderStockOutDetailAllBybillNo ("");
                dGridorderStockOutDetail.DataSource = bindingSourceorderStockOutDetail;
                DcolumnSet.colSet(moduleInfo.orderStockOutDetail , dGridorderStockOutDetail);
                baseSet.Query q = new baseSet.Query(null, dhDataAcess.baseBLL.dataConn.connOpen());
                bindingSourceorderStockOutMaster.DataSource = q.sqlQueryLikeAllToTable(moduleInfo.packStockInMaster  , textbillNoQuery.Text.ToString().Trim());
                dGridorderStockOutMaster.DataSource = bindingSourceorderStockOutMaster;
                //DcolumnSet.colSet(moduleInfo.productList, dGridProductList);
                DcolumnSet.colSet(moduleInfo.orderStockOutMaster , dGridorderStockOutMaster);
            }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message.ToString());
             }
        }

        //刷新按钮
        private void buttupdate_Click(object sender, EventArgs e)
        {
            getData();
        }
        //工具栏退出按钮，退出窗口
        private void toolExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //datagridview第一列显示行号
        private void dGridorderStockOutMaster_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
        }
        //datagridview第一列显示行号
        private void dGridorderStockOutDetail_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
        }
        private void getData()
        {
            orderStockOutDetailTableAdapter orderStockOutDetailAdapter = new orderStockOutDetailTableAdapter();
            orderStockOutMasterTableAdapter orderStockOutMasterAdapter = new orderStockOutMasterTableAdapter();
            produceDataSet orderStockOut = new produceDataSet();
            orderStockOutDetailAdapter.FillorderStockOutDetailALL(orderStockOut.orderStockOutDetail);
            orderStockOutMasterAdapter.FillorderStockOutMasterALL(orderStockOut.orderStockOutMaster);
            bindingSourceorderStockOutMaster.DataSource = orderStockOut;
            bindingSourceorderStockOutMaster.DataMember = "orderStockOutMaster";
            dGridorderStockOutMaster.DataSource = bindingSourceorderStockOutMaster;

            DcolumnSet.colSet(moduleInfo.orderStockOutMaster, dGridorderStockOutMaster);
            toolStripStatusLabel1.Text = "订单出库主表数据总条数:" + dGridorderStockOutMaster.RowCount.ToString() + "条";
            bindingSourceorderStockOutDetail.DataSource = bindingSourceorderStockOutMaster;
            bindingSourceorderStockOutDetail.DataMember = "fk_orderStockOutDetail_billNo";
            dGridorderStockOutDetail.DataSource = bindingSourceorderStockOutDetail;
            DcolumnSet.colSet(moduleInfo.orderStockOutDetail, dGridorderStockOutDetail);
            toolStripStatusLabel3.Text = "订单出库从表数据总条数:" + dGridorderStockOutDetail.RowCount.ToString() + "条";

            sysDataSet.modulAaccessDataTable modulAaccess = powerSet.modulPower(moduleInfo.orderStockOutMaster );
            foreach (sysDataSet.modulAaccessRow modulAaccessRow in modulAaccess)
            {
                buttonQuery.Enabled = modulAaccessRow.showAc;
                toolAdd.Enabled = modulAaccessRow.addAc;
                toolEdit.Enabled = modulAaccessRow.editAc;
                toolDel.Enabled = modulAaccessRow.delAc;
                toolPrint.Enabled = modulAaccessRow.printAc;
                toolExport.Enabled = modulAaccessRow.exportAc;
                //buttImport.Enabled = modulAaccessRow.importAc;

            }
        }

        private void toolExport_Click(object sender, EventArgs e)
        {
            FrmExcelExport frm = new FrmExcelExport(moduleInfo.orderStockOutMaster , dGridorderStockOutMaster , "订单成品出库信息");
            frm.ShowDialog();
        }

        private void 打印送货单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string billNo = dGridorderStockOutMaster.CurrentRow.Cells["billNo"].Value.ToString();
            Report r = new Report();
            DataSet ds = new DataSet();
            ds.Tables.Add(orderStockOut.getV_orderStockOutAllBybillNo (billNo));
            r.Load(@"E:\东华软件项目\报表\orderstockout.frx");
            //r.Load(@"\\192.168.0.27\东华软件\报表\orderstockout.frx");
            r.RegisterData(ds);
            //r.Design();
            r.Show();
        }

        private void 打印香港送货单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string billNo = dGridorderStockOutMaster.CurrentRow.Cells["billNo"].Value.ToString();
            Report r = new Report();
            DataSet ds = new DataSet();
            ds.Tables.Add(orderStockOut.getV_orderStockOutAllBybillNo(billNo));
            //r.Load(@"E:\东华软件项目\报表\orderstockoutxianggang.frx");
           r.Load(@"\\192.168.0.27\东华软件\报表\orderstockoutxianggang.frx");
            r.RegisterData(ds);
          //  r.Design();
            r.Show();
        }

        private void 打印河源送货单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string billNo = dGridorderStockOutMaster.CurrentRow.Cells["billNo"].Value.ToString();
            Report r = new Report();
            DataSet ds = new DataSet();
            ds.Tables.Add(orderStockOut.getV_orderStockOutAllBybillNo(billNo));
            //r.Load(@"E:\东华软件项目\报表\orderstockoutheyuan.frx");
            r.Load(@"\\192.168.0.27\东华软件\报表\orderstockoutheyuan.frx");
            r.RegisterData(ds);
           // r.Design();
            r.Show();
        }

        private void dGridorderStockOutMaster_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string billNo = dGridorderStockOutMaster.Rows[dGridorderStockOutMaster.CurrentRow.Index].Cells[1].Value.ToString();
            bindingSourceorderStockOutDetail.DataSource = orderStockOut.getsorderStockOutDetailAllBybillNo (billNo);
            dGridorderStockOutDetail.DataSource = bindingSourceorderStockOutDetail;
            DcolumnSet.colSet(moduleInfo.orderStockOutDetail , dGridorderStockOutDetail);
        }
    }
}
